<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2021/3/1
 * Time: 11:19
 */

class model_product extends L {

    /**
     * 描述 : 获取产品信息
     * 作者 : Zoro.Zhu
     */
    public static function getPdmInfo($skuId,$mark = 'base',$field = []){
        $sql = "SELECT
            `field`, `data`
        FROM
            `sku_info_field`
        WHERE
            `skuId` = '{$skuId}'
        AND `mark` = '{$mark}'";
        !empty($field) && $sql .= " AND field IN ( '" . join("','", $field) . "' )";
        $temp = L::sql($sql, 'product');
        $result = [];
        //解析原属性
        foreach ($temp as &$v) {
            $result[$v['field']] = json_decode($v['data'], true);
        }
        return $result;
    }

    /**
     * 描述 : 获取图片
     * 作者 : Zoro.Zhu
     */
    public static function getImg($sku){
        $sql = "SELECT
            sku_pic_pack.genre,
            sku_pic_pack.picClass,
            sku_pic_pack.skuId,
            sku_pic.url,
            sku_pic.path 
        FROM
            `sku_pic_pack`
            JOIN `sku_pic` ON `sku_pic_pack`.`picId` = `sku_pic`.`picId` 
        WHERE
            sku_pic_pack.skuId = '{$sku}' 
            AND sku_pic_pack.mark = 'base' 
        ORDER BY
            sku_pic_pack.genre DESC";
        return L::sql($sql, 'product');
    }

    /**
     * 描述 : 获取sku的供应商
     * 作者 : Adam Zhao
     */
    public static function getSeller($skuId)
    {
        $sql = "SELECT
            sls.`seller`,
            sia.`aliasCode`,
            sia.`company`
        FROM
            `sku_link_seller` sls
            LEFT JOIN `seller_info_attr` sia ON sls.`seller` = sia.`seller` 
        WHERE
            sls.`skuId` = '{$skuId}'";
        $data = L::sql($sql, 'product');
        $sellerArr = [];
        if (!empty($data) && is_array($data)) {
            $sellerArr = array_combine(array_column($data, 'seller'), array_column($data, 'aliasCode'));
            $sellerStr = implode("','", array_column($data, 'seller'));
            $nameArr = array_combine(array_column($data, 'seller'), array_column($data, 'company'));
        } else {
            return [];
        }
        $sql = "SELECT
            `skuId`,
            `seller`,
            `sellerSku`,
            `skuYields`,
            `buyPrice`,
            `buyMultiple`,
            `buyMinQty`,
            `state`,
            `data`
        FROM
            `seller_link_skus`
        WHERE
            `seller` IN ('{$sellerStr}')
            AND `skuId` = '{$skuId}'";
        $data = L::sql($sql, 'product');
        if (!empty($data) && is_array($data)) {
            foreach ($data as &$value) {
                $json = !empty($value['data']) ? json_decode($value['data'], true) : [];
                $value['skuUrl'] = isset($json['skuUrl']) ? $json['skuUrl'] : '';
                $value['isDefault'] = isset($json['isDefault']) ? ($json['isDefault'] == 1 ? '是' : '否') : '否';
                $value['aliasCode'] = isset($sellerArr[$value['seller']]) ? $sellerArr[$value['seller']] : $value['seller'];
                $value['company'] = isset($nameArr[$value['seller']]) ? $nameArr[$value['seller']] : '-';
                $value['buyPrice'] = floatExpandHundred($value['buyPrice'], 2);
            }
        }
        return $data;
    }

}